package lookas.controllers;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;

import lookas.model.Customer;
import lookas.repositories.CustomerRepository;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
@RequestMapping("/customer")
public class CustomerController {

    @Resource(name = "customerMockRepository")
    private CustomerRepository customerRepository;

    @Autowired
    private Validator          validator;

    private int                perSite = 5;

    @RequestMapping(method = RequestMethod.GET)
    public String get(Model model) {
        return get(1, model);
    }

    @RequestMapping(value = "/test/{pesel}", method = RequestMethod.GET)
    @ResponseBody
    public Customer get(@PathVariable String pesel) {
        Customer customer = customerRepository.findCustomerByPesel(pesel);
        return customer;
    }

    @RequestMapping(value = "{siteId}", method = {RequestMethod.GET, RequestMethod.POST})
    public String get(@PathVariable int siteId, Model model) {
        long size = customerRepository.size();
        int sites = 0;
        if (size % perSite > 0)
            sites = 1;
        sites += (int) (size / perSite);
        siteId = siteId < 1 ? 1 : siteId;
        siteId = siteId > sites ? sites : siteId;
        int offset = (siteId - 1) * perSite;
        Collection<Customer> c = customerRepository.findLimitedCustomers(offset, perSite);
        model.addAttribute("customerList", c);
        model.addAttribute("sites", sites);
        model.addAttribute("current", siteId);
        model.addAttribute("previous", siteId - 1);
        model.addAttribute("next", siteId + 1);
        model.addAttribute("user", new Customer("first", "last", "12345678901"));
        return "customer";
    }

    @RequestMapping(value = "/create/{create}", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, String> save(@RequestBody Customer customer, @PathVariable boolean create) {
        Map<String, String> ret = new HashMap<String, String>();
        Customer c = customerRepository.findCustomerByPesel(customer.getPesel());
        if (create) {
            if (c != null) {
                ret.put("blad", "");
                return ret;
            }
        } else {
            if (c == null) {
                ret.put("blad", "");
                return ret;
            }
        }

        Set<ConstraintViolation<Customer>> validate = validator.validate(customer);
        if (validate.size() > 0) {
            for (ConstraintViolation<Customer> v : validate) {
                ret.put(v.getPropertyPath().toString(), v.getMessage());
            }
        } else {
            customerRepository.save(customer);
        }
        return ret;
    }

    // @RequestMapping(method = RequestMethod.POST, value = "{siteId}")
    // public String create(@Valid Customer customer, BindingResult result) {
    //
    // Customer c = repository.findCustomerByPesel(customer.getPesel());
    // if (c == null) {
    // repository.save(customer);
    // }
    // return "ok";
    // }

    @RequestMapping(method = RequestMethod.DELETE)
    @ResponseBody
    public String delete(@RequestBody String pesel) {
        Customer c = customerRepository.findCustomerByPesel(pesel);
        customerRepository.delete(c);
        return "ok";
    }

    public void setCustomerRepository(CustomerRepository customerRepository) {
        this.customerRepository = customerRepository;
    }

    public void setPerSite(int perSite) {
        this.perSite = perSite;
    }
}